package cn.cdeden.knowledge.dubbo;

import cn.cdeden.common.oss.enumd.OssBucketOneEnum;
import cn.cdeden.common.oss.enumd.OssModuleOperate;
import cn.cdeden.common.oss.enumd.OssModuleType;
import cn.cdeden.knowledge.domain.FileTemplateTree;
import cn.cdeden.knowledge.domain.dto.FileTemplateTreeDto;
import cn.cdeden.knowledge.enums.FileSuffixTypeEnum;
import cn.cdeden.knowledge.service.FileTemplateTreeService;
import cn.cdeden.resource.api.OssUploadService;
import cn.cdeden.resource.api.domain.*;
import cn.cdeden.system.api.RemoteUserService;
import cn.cdeden.system.api.model.LoginUser;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 文件模版上传的实现
 */
@Slf4j
@Service
@RequiredArgsConstructor
@DubboService(group = "template" ,version = "1.0.0")
public class OssUploadTemplateServiceAImpl implements OssUploadService {

    private final FileTemplateTreeService fileTemplateTreeService;

    @DubboReference(check = false)
    private final RemoteUserService remoteUserService;


    @Override
    public RemoteFile getFileDataInfo(String moduleId, Long userId, JSONObject info, OssModuleOperate operate) {
        FileTemplateTreeDto fileTemplateTreeDto = fileTemplateTreeService.findById(Long.valueOf(moduleId));
        RemoteFile remoteFile = new RemoteFile();
        if (StringUtils.isNotBlank(fileTemplateTreeDto.getStorageId())) {
            remoteFile.setOssId(Long.valueOf(fileTemplateTreeDto.getStorageId()));
        }
        remoteFile.setVersionId(fileTemplateTreeDto.getEdition());
        remoteFile.setVersionName(fileTemplateTreeDto.getEditionName());
        remoteFile.setFileSize(fileTemplateTreeDto.getSize());
        remoteFile.setFileSuffix(fileTemplateTreeDto.getSuffix());
        remoteFile.setModuleType(OssModuleType.Template.getCode());
        remoteFile.setName(fileTemplateTreeDto.getRealName());
        remoteFile.setOriginalName(fileTemplateTreeDto.getRealName());

        //TODO 修改文件的预览次数
        fileTemplateTreeService.update(new LambdaUpdateWrapper<FileTemplateTree>().eq(FileTemplateTree::getId, moduleId).setSql("preview = preview + 1"));

        return remoteFile;
    }

    @Override
    public RemoteOssUpload verifyData(RemoteOssUpload info, Long userId) {
        info.setBrandName(OssBucketOneEnum.TEMPLATE_BUCKET.getCode());
        return info;
    }

    @Override
    public RemoteFilesBucketPath upload(RemoteFilesUpload upload, Long userId) {
        LoginUser user = remoteUserService.getUserInfo(userId);


        FileTemplateTree fileTemplateTree = new FileTemplateTree();
        if (upload.getParentId() != null) {
            fileTemplateTree.setParentId(Long.valueOf(upload.getParentId()));
        }
        fileTemplateTree.setSuffix(upload.getFileSuffix());
        fileTemplateTree.setRealName(upload.getFileName());
        fileTemplateTree.setType(FileSuffixTypeEnum.fromExtension(upload.getFileSuffix()).name());
        fileTemplateTree.setStorageId(upload.getStorageId());
        fileTemplateTree.setStorageType("minio");
        fileTemplateTree.setState("personal");
        fileTemplateTree.setCreateId(userId.toString());
        fileTemplateTree.setCreateBy(user.getUsername());
        fileTemplateTree.setCreateTime(new Date());
        fileTemplateTree.setLibraryType("template");

        //存入数据
        fileTemplateTreeService.save(fileTemplateTree);
        return RemoteFilesBucketPath.of(OssBucketOneEnum.TEMPLATE_BUCKET.getCode()).setUserList(Arrays.asList(user.getUserId().toString()));
    }

    @Override
    public List<RemoteFilesDownload> downloadFile(List<String> idList, Long userId, JSONObject jsonObject) {
        return null;
    }
}
